<?php

/***
 * Class CW_LocaleClass grants a system the
 * ability to allow internationalization.
 ***/
class CW_LocaleClass {

	/***
	 * Title of the locale to load.
	 * @type String
	 ***/
	private $locale_to_load;


	/***
	 * File path to the directory where the chosen locale is stored.
	 * @type String
	 ***/
	private $path_to_locale_directory;


	/***
	 * This holds all loaded locale definitions.
	 * @type Array
	 ***/
	private $locale_definitions;


	// ! Constructor

	/***
	 * Insantiates object.
	 * @param $Error Error handler passed by reference.
	 ***/
	public function __construct($locale = 'en')
	{
		$this->path_to_locale_directory = getcwd();
		$this->locale_to_load = $locale;
		$this->locale_definitions = array();
	}


   // ! Mutator

   /**
	* Sets the path to the locale directory and uses
	* some error checking to make sure it's correct.
	*
	* @param $path_to_locale_directory The path to the locale directory.
	* @return Bool
	*/
	public function setPathToLocaleDirectory($path_to_locale_directory)
	{
		$path_to_locale_directory = preg_replace('#[/|\\\]+$#', '', $path_to_locale_directory) . DIRECTORY_SEPARATOR;

		if(is_dir($path_to_locale_directory))
		{
			$this->path_to_locale_directory = $path_to_locale_directory . $this->locale_to_load . DIRECTORY_SEPARATOR;

			if(false == file_exists($this->path_to_locale_directory))
			{
				throw new CoreException("Path to chosen locale directory, {$this->path_to_locale_directory}, could not be located.");
			}

			return true;
		}

		throw new CoreException("Path to chosen locale directory, {$path_to_locale_directory}, could not be located.");

		return false;
	}


   // ! Executor

   /**
	* Loads a chosen locale file into memory.
	*
	* @param none
	* @return Bool
	*/
	public function loadDefinitions($locale_file_name, $ignore_missing_files = true)
	{
		if(false == file_exists($this->path_to_locale_directory . $locale_file_name) && false == $ignore_missing_files)
		{
			throw new CoreException("Path to chosen locale file, {$locale_file_name}, could not be located.");
		}

		if(false == file_exists($this->path_to_locale_directory . $locale_file_name) && $ignore_missing_files)
		{
			return true;
		}

		$definitions = array();

		include_once $this->path_to_locale_directory . $locale_file_name;

		$this->locale_definitions = array_merge($this->locale_definitions, $definitions);

		return true;
	}


   // ! Accessor

   /**
	* Returns a chosen locale definition.
	*
	* @param $locale_definition locale definition to load.
	* @return Mixed
	*/
	public function getLocaleDefinition($locale_definition, $replacements = array())
	{
		if(false == isset($this->locale_definitions[$locale_definition]))
		{
			return false;
		}

		if($replacements)
		{
			$string = $this->locale_definitions[$locale_definition];

			eval("\$this->locale_definitions[\$locale_definition] =  sprintf('$string', '" . implode("','", $replacements) . "');");
		}

		return $this->locale_definitions[$locale_definition];
	}


   // ! Mutator

   /**
	* May be used to add a new definition or update an
	* existing one.
	*
	* @param $definition_title Name of definition to return.
	* @param $definition_value Value of chosen definition.
	* @return String
	*/
	public function setLocaleDefinition($definition_title, $definition_value)
	{
		$this->locale_definitions[$definition_title] = $definition_value;

		return true;
	}


   // ! Accessor

   /**
	* Returns all currently loaded locale definitions.
	*
	* @param none
	* @return Array
	*/
	public function getAll()
	{
		return $this->locale_definitions;
	}
}

?>